<?php

class Meeting extends CCMIS_Controller 
{

	public function __construct() {
		parent::__construct();

        $this->load->model('model_facultymember');
        $this->load->model('model_facultymeeting');
        $this->load->model('model_facultymeetingstatus');
        $this->load->model('model_facultymeetingmember');

    }

    public function index()
    {
        $this->view();
    }

    public function view()
    {
        //main page to view all meetings

        //$table = $this->FMIS_Meeting->get_statusDesc();
        $profiles = $this->model_facultymember->getAll();
        $meetings = $this->model_facultymeeting->getAll();
        $statuses = $this->model_facultymeetingstatus->getAll();

        $meetingsNew = array();

        foreach($meetings as $meeting )
        {
            $meetingNew = $meeting;
            $meetingNew['Status'] = $this->model_facultymeetingstatus->getDescById($meeting['StatusID']);
            $meetingNew['RecorderName'] = $this->model_facultymember->getNameAppendedByID($meeting['RecorderID']);

            array_push($meetingsNew, $meetingNew);
        }


        $data = array(
            'page_title'  => 'Meeting',
            //'tbl' => $table,
            'statuses' => $statuses,
            'profiles' => $profiles,
            'meetings' => $meetingsNew
        );

    	$this->setup_view('fmis/meeting/view', $data);
    }

    public function viewmeeting($meetingID = 14344)
    {
        //view ng meeting in detail
        $meeting = $this->model_facultymeeting->getById($meetingID);
        $meeting['RecorderName'] = $this->model_facultymember->getNameAppendedByID($meeting['RecorderID']);
        $meeting['Date'] = $this->model_facultymeeting->getDateById($meetingID);
        $meeting['Time'] = $this->model_facultymeeting->getTimeById($meetingID);
        $meetingAttendees = $this->model_facultymeetingmember->getByMeetingId($meetingID);

        $meetingAttendeesRetval = array();
        foreach($meetingAttendees as $attendee)
        {
            $attendeeRetval = $attendee;
            $attendeeRetval['Name'] = $this->model_facultymember->getNameAppendedByID($attendee['MemberID']);

            array_push($meetingAttendeesRetval, $attendeeRetval);
        }


        $data = array(
            'meetingID' => $meetingID,
            'details' => $meeting,
            'attendees' => $meetingAttendeesRetval,
        );
        $this->setup_view('fmis/meeting/viewmeeting', $data);
        //*/
    }

    public function addmeeting()
    {
        $recorderID = $this->session->userdata('user_id');
        $meetingName = $this->input->post('edit_meetingname');
        $meetingDate = $this->input->post('edit_date');
        $meetingTime = $this->input->post('edit_time');
        $meetingLocation = $this->input->post('edit_location');
        $meetingStatus = $this->input->post('edit_status');

        $meetingAgenda = $this->input->post('edit_agenda');
        $meetingAttendees = $this->input->post('id_list');

        $hour = date_format(date_create($meetingTime), 'h');
        $min =  date_format(date_create($meetingTime), 'i');
        $sec =  date_format(date_create($meetingTime), 's');
        $mer =  date_format(date_create($meetingTime), 'a');

        if($mer == 'pm' && $hour != 12)
        {
            $hour += 12;
        }
        else if($mer == 'am' && $hour == 12)
        {
            $hour = 0;
        }

        $meetingTime = $hour.':'.$min.':'.$sec;
        $datetime = date_format(date_create($meetingDate.$meetingTime), 'Y-m-d h:i:s');

        $this->model_facultymeeting->add($recorderID, $meetingName, $meetingAgenda, $meetingLocation, $datetime, $meetingStatus);
        $meetingAdded = $this->model_facultymeeting->getIdBySpecifics($recorderID, $meetingName, $meetingAgenda, $meetingLocation, $datetime, $meetingStatus);

        //echo $meetingAdded['MeetingID'];
        $meetingAttendees = explode(' ', $meetingAttendees);

        foreach($meetingAttendees as $attendee)
        {
            $this->model_facultymeetingmember->add($meetingAdded, $attendee);
        }

        //$this->setup_view('fmis/meeting/addmeeting');
    }

    public function editmeeting($meetingID = 14344)
    {        
        $data = array(
            'meetingID' => $meetingID,
            'meetingName' => 'This is meeting name'
        );
        $this->setup_view('fmis/meeting/editmeeting', $data);
    }

    public function deletemeeting($meetingID = 14344)
    {
        $data = array(
            'meetingID' => $meetingID
        );
        $this->setup_view('fmis/meeting/deletemeeting', $data);
    }
}

/* End of file pages.php */
/* Location: ./application/controllers/pages.php */